<?php
namespace app\admin\controller;
use app\admin\model\GoodsProp;
use app\admin\model\GoodsPropValue;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class Sxgl extends BaseController
{
    public function sxlbs()
    {
        $getParam = Request::get('', '', 'trim,htmlspecialchars,strip_tags');
        $lent = \app\admin\model\GoodsPropValue::getPropValue();
        $list = \app\admin\model\GoodsProp::getProp($getParam, $getParam, $lent);
        View::assign($getParam);
        View::assign('lent', $lent);
        View::assign('list', $list);
        $empty = "<tr><td colspan='9' style='text-align: center;background-color: #aaaaaa'>暂无数据</td></tr>";
        View::assign('empty', $empty);
        return View::fetch();
    }

    public function sxlbadd()
    {
        $lent = \app\admin\model\GoodsPropValue::getPropValue();
        View::assign('lent', $lent);
        if (Request::isPost()) {
            $postParam = Request::post('', '', 'trim,htmlspecialchars,strip_tags');
            try {
                validate(\app\admin\validate\GoodsProp::class)->check($postParam);
            } catch (\think\exception\ValidateException $e) {
                $this->error($e->getMessage());
            }
            if (isset($postParam['qwert'])) {
                $url = 'sxlbadd';
                unset($postParam['qwert']);
            }
            if (isset($postParam['qwer'])) {
                $url = 'sxlbs';
                unset($postParam['qwer']);
            }
            if ('' == $postParam['pname']) {
                echo "<script>alert('请输入属性名!');history.back();</script>";
                return View::fetch();
            }
            $s = array_key_exists('pvalues', $postParam);
            if ("" == $s) {
                echo "<script>alert('请选择属性值!');history.back();</script>";
                return View::fetch();
            }
            if ($postParam['sort_order'] < 0 or $postParam['sort_order'] > 255) {
                echo '<script>alert("排序值需在0~255之间！");history.back();</script>';
                return View::fetch();
            }
            $shuxing = new \app\admin\model\GoodsProp();
            $db = $shuxing->add($postParam);
            if ($db) {
                $this->success("成功", url("{$url}"));
            } else {
                $this->error("失败！原因" . $shuxing->getError());
            }
            if ($db) {
                echo '成功!' . $db;
            } else {
                echo '失败!' . $shuxing->getError();
            }
        } else {
            return View::fetch();
        }
    }

    public function sxlbedit()
    {
        if (Request::isPost()) {
            $post = Request::post('', '', 'trim,htmlspecialchars,strip_tags');
            $get = Request::get('', '', 'trim,htmlspecialchars,strip_tags');
            try {
                validate(\app\admin\validate\GoodsProp::class)->scene('edit')->check($post);
            } catch (\think\exception\ValidateException $e) {
                $this->error($e->getMessage());
            }
            if (isset($get['id'])) unset($get['id']);
            $param = Request::param();
            $post['pvalues'] = implode(',', $post['pvalues']);
            $sxglObj = new GoodsProp();
            $result = $sxglObj->upt($post);
            if ($result) {
                $this->success('修改成功', url('sxlbs'));
            } else {
                $this->error('修改失败');
            }
        } else {
            //id值
            $get = Request::get('', '', 'trim,htmlspecialchars,strip_tags');
            $id = $get['id'] ?? '';
            $sxglObj = GoodsProp::find($id)->getData();
            if (empty($sxglObj)) {
                $this->error('参数错误', url('sxlbs'));
            }
            View::assign($sxglObj);
            $lent = GoodsPropValue::getPropValue();
        }
        return View::fetch('', ['lent' => $lent]);
    }

    public function del()  //属性删除
    {
        $id = Request::get('id','','trim,htmlspecialchars,strip_tags');
        $get = Request::get('','','trim,htmlspecialchars,strip_tags');
        if (isset($get[0]) && 1 == $get[0])     unset($get[0]);
        if (isset($get['id']))        unset($get['id']);

        if (empty($id))     $this->error('非法参数','sxlbs');
        try{
            GoodsProp::destroy($id);
        }catch (\Exception $e){
            $this->error('删除失败',url('sxlbs',$get));
        }
        $this->success('删除成功',url('sxlbs',$get));
    }
//    public function sxlbs1(){
//        $prop_value_keyword = Request::get('prop_value/a',[],'trim,htmlspecialchars,strip_tags');
//        $keywords = Request::get('keywords','','trim,htmlspecialchars,strip_tags');
//
//
//
//
//        $list="";
//        View::assign("list",$list);
//        $prop_value_list = Db::table('ecm_goods_prop_value')
//                            ->field('vid,prop_value')
//                            ->where('status',1)
//                            ->order('sort_order')
//                            ->select();
//        $prop_value_arr = [];
//        foreach ($prop_value_list as $value){
//            $prop_value_arr[$value['vid']] = $value['prop_value'];
//        }
//        $db=Db::table('ecm_goods_prop')->field(true);
//        if (is_array($prop_value_keyword) && count($prop_value_keyword)>0){
//            foreach ($prop_value_keyword as $v){
//                $db->where('pvalues','find in set',$v);
//            }
//        }
//        View::assign('prop_value_keyword',$prop_value_keyword);
//
//        if ('' !==$keywords){
//            $db->where('pname','like','%'.$keywords.'%');
//        }
//        View::assign('keywords',$keywords);
//
//
//
//        $arr=[
//            'list_rows'=>2,
//            'query' =>Request::instance()->param(),
//        ];
//        $agg = $db->paginate($arr);
//        $page = $agg->render();
//        View::assign('page',$page);
//            $list = $agg->toArray()['data'];
//        foreach ($list as $key=>$value){
//            $arrTmp=explode(',',$value['pvalues']);
//            $list[$key]['pvalues_zh'] = '';
//            foreach ($arrTmp as $v){
//               $list[$key]['pvalues_zh'] .= $prop_value_arr[$v] . ',';
//            }
//            $list[$key]['pvalues_zh'] = trim($list[$key]['pvalues_zh'],',');
//        }
//        View::assign('list',$list);
//        View::assign('prop_value_arr',$prop_value_arr);
//
//        $empty = "<tr><td colspan='7'>暂无数据！</td></tr>";
//        View::assign('empty',$empty);
//        return View::fetch();
//    }
//

}